#include "server.h"
#include <QSettings>
#include <QCoreApplication>

Server::Server(QObject *parent) : QTcpServer(parent){
    connectedClients = 0;
}

Server::~Server(){
 //   stopServer();
}

void Server::startServer(){

    QString  dirPath =  QCoreApplication::applicationDirPath() +"/config/ftp.ini";
    QSettings *settings = new QSettings(dirPath, QSettings::IniFormat);
     settings->beginGroup("Ftp");
    quint16 port;

    port = (settings->value("serverPort").isNull() ? 1234 : settings->value("serverPort").toInt());

    if(!this->listen(QHostAddress::Any, port)){
        message("Couldn't start server... Port already in use?");
    } else {
        message("Listening...");
    }

    settings->endGroup();
}

void Server::stopServer(){
    this->close();
    emit kickAll();
}

void Server::incomingConnection(qint32 socketDescriptor){
    //  QSettings settings;
      QString  dirPath =  QCoreApplication::applicationDirPath() +"/config/ftp.ini";
      QSettings *settings = new QSettings(dirPath, QSettings::IniFormat);
       settings->beginGroup("Ftp");
   //   qDebug() << settings->value("maxUsers");

      // TODO FIXEN!

      Client *client = new Client(this);

      connect(client, SIGNAL(message(QString)), this, SLOT(message(QString)));
      connect(client, SIGNAL(info(QString,QString, QString)), this, SLOT(getInfo(QString,QString, QString)));
      connect(client, SIGNAL(removeFromTable(int)), this, SLOT(removeFromTable(int)));
      connect(client, SIGNAL(clientMessage(QString,QString)), this, SLOT(clientMessage(QString, QString)));

      connect(this, SIGNAL(deleteUser(int)), client, SLOT(kickClient(int)));
      connect(this, SIGNAL(kickAll()), client, SLOT(kickAll()));

     client->setCurrentDir(settings->value("rootPath","./").toString());
     client->setwelcomeMessage(settings->value("welcomeMessage","").toString());
     client->setSocket(socketDescriptor);
     client->setallowAnonUsers(settings->value("allowAnonUsers").toBool());
      connectedClients += 1;


      if(connectedClients >= settings->value("maxUsers").toInt()){
          message("Server full, closing...");
          this->close();
      }
     settings->endGroup();
}

void Server::message(QString msg){
    emit sendToLogServer(msg);
}

void Server::clientMessage(QString username, QString message){
    emit sendToLogClient(username, message);
}

void Server::getInfo(QString id,QString ip, QString username){
    emit sendToTable(id, ip, username);
}

void Server::deleteUserServer(int id){
    emit deleteUser(id);
}

void Server::removeFromTable(int id){
    emit removeFromTableSignal(id);
    connectedClients -= 1;

    //  QSettings settings;
      QString  dirPath =  QCoreApplication::applicationDirPath() +"/config/ftp.ini";
      QSettings *settings = new QSettings(dirPath, QSettings::IniFormat);
       settings->beginGroup("Ftp");
    if(connectedClients < settings->value("maxUsers").toInt()){
        quint16 port;
        port = (settings->value("serverPort").isNull() ? 1234 : settings->value("serverPort").toInt());
        this->listen(QHostAddress::Any, port);
        message("More room, opening again...");
    }
     settings->endGroup();
}
